package com.teach.service.impl.photo;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teach.commons.Id;
import com.teach.dto.LoginUser;
import com.teach.dto.PhotoFormDTO;
import com.teach.dto.PhotoPicSearch;
import com.teach.dto.PhotoPopularAuthorDTO;
import com.teach.entity.PhotoPic;
import com.teach.mapper.PhotoPicMapper;
import com.teach.service.FileService;
import com.teach.service.photo.PhotoPicService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class PhotoPicServiceImpl implements PhotoPicService{

	@Autowired
	private PhotoPicMapper mapper;
	
	@Autowired
	private FileService fileService;
	
	@Autowired
	private Id id;
	
	@Override
	public int deleteByPrimaryKey(Long photoPicId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insert(PhotoPic record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertSelective(LoginUser user, PhotoFormDTO form,MultipartFile img) {
		String save = fileService.save(img, true);
		log.debug("save {}", save);
		String saveImage = fileService.saveImage(img);
		log.debug("saveImage {}", saveImage);
		String saveThumbnail = fileService.saveThumbnail(img);
		log.debug("saveThumbnail {}", saveThumbnail);
		PhotoPic photoPic = new PhotoPic();
		//存储路径
		photoPic.setPhotoThumbnailPicPath(saveThumbnail);
		photoPic.setPhotoWatermarkPicPath(saveImage);
		photoPic.setPhotoOriginalPicPath(save);
		
		//作者用户名
		photoPic.setPhotoWriterId(user.getUserName());
		
		photoPic.setPhotoPicClassId(form.getAlbumId());
		photoPic.setPhotoPicTitle(form.getPhotoName());
		if(!StringUtils.isEmpty(form.getPhotoMemo())) {
			photoPic.setPhotoPicRemark(form.getPhotoMemo());
		}
		
		photoPic.setPhotoReleaseTime(new Date());
		photoPic.setPhotoPicStatus("1");
		
		return mapper.insertSelective(photoPic);
	}

	@Override
	public PhotoPic selectByPrimaryKey(Long photoPicId) {
		// TODO Auto-generated method stub
		return mapper.selectByPrimaryKey(photoPicId);
	}

	@Override
	public int updateByPrimaryKeySelective(PhotoPic record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateByPrimaryKey(PhotoPic record) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<PhotoPic> selectAll(int index,int len) {
		// TODO Auto-generated method stub
		return mapper.selectAll(index, len);
	}

	@Override
	public List<PhotoPic> selectGreatDESC() {
		// TODO Auto-generated method stub
		return mapper.selectGreatDESC();
	}

	
	@Override
	public List<PhotoPic> selectPicDetails(String title, String writerId, Long picClassId) {
		 return mapper.selectPicDetails(title, writerId, picClassId);
	}
	
	@Cacheable(value = "PhotoPicService", key = "'selectByWriterId' + #photoWriterId")
	@Override
	public List<PhotoPic> selectByWriterId(String photoWriterId) {
		return mapper.selectByWriterId(photoWriterId);
	}

	@Override
	public Long selectPicTitle(String photoPicTitle) {
		// TODO Auto-generated method stub
		return mapper.selectPicTitle(photoPicTitle);
	}

	@Override
	public PhotoPopularAuthorDTO  selectPopularAuthor(String photoReleaseTime,String userName) {
		// TODO Auto-generated method stub
		return mapper.selectPopularAuthor(photoReleaseTime, userName);
	}

	@Override
	public Integer selectWorksCount(String photoReleaseTime, String userName) {
		// TODO Auto-generated method stub
		return mapper.selectWorksCount(photoReleaseTime, userName);
	}

	@Override
	public PhotoPic selectPicDetailsDESC(String title, String writerId, Long picClassId) {
		// TODO Auto-generated method stub
		return mapper.selectPicDetailsDESC(title, writerId, picClassId);
	}

	@Override
	public List<PhotoPic> selectType(Long picClassId) {
		// TODO Auto-generated method stub
		return mapper.selectType(picClassId);
	}
	
	@Override
	public PageInfo<PhotoPic> pageSearch(PhotoPicSearch search) {
		return PageHelper.startPage(search.getCurrent(), search.getRowMax()).doSelectPageInfo(()->mapper.pageSearch(search));
	}

	@Override
	public PhotoPopularAuthorDTO selectAuthorInfo(String userName) {
		// TODO Auto-generated method stub
		return mapper.selectAuthorInfo(userName);
	}
	
}
